<h1>Survey download statistics</h1>
<div class="tabber" id="statistics-tab">
	<div id="user-stats-tab" class="tabbertab listings_blank" title="Survey downloads by user">
		<h3>Survey downloads by user</h3>
<!-- active users -->
<!-- week -->
<div class="statistics-table">
	<h3 id="heading">Users active in the last week</h3>
<% unless @download_hash_week.empty? %>

<table>
	<!-- <caption>Users active in the last week</caption> -->
	<thead>
		<tr><th>User</th><th>Downloads</th></tr>
	</thead>
	<tbody>
<% @download_hash_week.each_key do |key| %>
<tr><td><% unless key == "unknown_user" %><%= User.find(key).person.name %></td><% else %>Unregistered user<% end %><td>
	<% @download_hash_week[key].each_key do |survey_key| %>
		<%= Survey.find(survey_key).title %>(<%= @download_hash_week[key][survey_key] %>)
	<% end %>
	</td>
<% end %>
</tbody>
</table>
<% else %>
No user activity in the last week
<% end %>
</div>
<!-- month -->
<div class="statistics-table">
	<h3 id="heading">Users active in the last month</h3>
<% unless @download_hash_month.empty? %>

<table>
	<!-- <caption>Users active in the last month</caption> -->
	<thead>
		<tr><th>User</th><th>Downloads</th></tr>
	</thead>
	<tbody>
<% @download_hash_month.each_key do |key| %>
<tr><td><% unless key == "unknown_user" %><%= User.find(key).person.name %></td><% else %>Unregistered user<% end %><td>
	<% @download_hash_month[key].each_key do |survey_key| %>
		<%= Survey.find(survey_key).title %>(<%= @download_hash_month[key][survey_key] %>)
	<% end %>
	</td>
<% end %>
</tbody>
</table>

<% else %>
No user activity in the last month
<% end %>
</div>
<!-- six months -->
<div class="statistics-table">
	<h3 id="heading">Users active in the last six months</h3>
<% unless @download_hash_six_months.empty? %>

<table>
	<!-- <caption>Users active in the last six months</caption> -->
	<thead>
		<tr><th>User</th><th>Downloads</th></tr>
	</thead>
	<tbody>
<% @download_hash_six_months.each_key do |key| %>
<tr><td><% unless key == "unknown_user" %><%= User.find(key).person.name %></td><% else %>Unregistered user<% end %><td>
	<% @download_hash_six_months[key].each_key do |survey_key| %>
		<%= Survey.find(survey_key).title %>(<%= @download_hash_six_months[key][survey_key] %>)
	<% end %>
	</td>
<% end %>
</tbody>
</table>

<% else %>
No user activity in the last six months
<% end %>
</div>
<!-- year -->
<div class="statistics-table">
	<h3 id="heading">Users active in the last year</h3>
<% unless @download_hash_year.empty? %>

<table>
	<!-- <caption>Users active in the last year</caption> -->
	<thead>
		<tr><th>User</th><th>Downloads</th></tr>
	</thead>
	<tbody>
<% @download_hash_year.each_key do |key| %>
<tr><td><% unless key == "unknown_user" %><%= User.find(key).person.name %></td><% else %>Unregistered user<% end %><td>
	<% @download_hash_year[key].each_key do |survey_key| %>
		<%= Survey.find(survey_key).title %>(<%= @download_hash_year[key][survey_key] %>)
	<% end %>
	</td>
<% end %>
</tbody>
</table>

<% else %>
No user activity in the last year
<% end %>
</div>
</div>
<div id="download-stats-tab" class="tabbertab listings_blank" title="Survey downloads over the last year">
	<h3>Survey downloads over the last year</h3>
<!-- last week -->
<div class="statistics-table">
<h3 id="heading">Last week</h3>
<div class="gviz-chart" id="week_chart_div"></div>
<% week_keys = @week.keys -%>
<% unless @week.empty? %>
<table>
	<!-- <caption>Downloads in the last week</caption> -->
	<thead>
		<tr><th>Survey</th><th>Number of downloads</th></tr>
	</thead>
	<tbody>
<% @week.each_key do |key| -%>
	<tr><td><%= Survey.find(key).title -%></td><td><%= @week[key] -%></td></tr>
<% end -%>
</tbody>
</table>
<% else %>
No downloads in the last week
<% end %>
</div>
<!-- last month -->
<div class="statistics-table">
<h3 id="heading">Last month</h3>
<div class="gviz-chart" id="month_chart_div"></div>
<% month_keys = @month.keys -%>
<% unless @month.empty? %>
<table class="statistics-table">
	<!-- <caption>Downloads in the last month</caption> -->
	<thead>
		<tr><th>Survey</th><th>Number of downloads</th></tr>
	</thead>
	<tbody>
<% @month.each_key do |key| -%>
	<tr><td><%= Survey.find(key).title -%></td><td><%= @month[key] -%></td></tr>
<% end -%>
</tbody>
</table>
<% else %>
No downloads in the last month
<% end %>
</div>
<!-- last six months -->
<div class="statistics-table">
<h3 id="heading">Last six months</h3>
<div class="gviz-chart" id="six_months_chart_div"></div>
<% six_months_keys = @six_months.keys -%>
<% unless @six_months.empty? %>
<table class="statistics-table">
	<!-- <caption>Downloads in the last six months</caption> -->
	<thead>
		<tr><th>Survey</th><th>Number of downloads</th></tr>
	</thead>
	<tbody>
<% @six_months.each_key do |key| -%>
	<tr><td><%= Survey.find(key).title -%></td><td><%= @six_months[key] -%></td></tr>
<% end -%>
</tbody>
</table>
<% else %>
No downloads in the last six months
<% end %>
</div>
<!-- last year -->
<div class="statistics-table">
<h3 id="heading">Last year</h3>
<div class="gviz-chart" id="year_chart_div"></div>
<% year_keys = @year.keys -%>
<% unless @year.empty? %>
<table class="statistics-table">
	<!-- <caption>Downloads in the last year</caption> -->
	<thead>
		<tr><th>Survey</th><th>Number of downloads</th></tr>
	</thead>
	<tbody>
<% @year.each_key do |key| -%>
	<tr><td><%= Survey.find(key).title -%></td><td><%= @six_months[key] -%></td></tr>
<% end -%>
</tbody>
</table>
<% else %>
No downloads in the last year
<% end %>
</div>
</div>
<div id="user-any-activity-tab" class="tabbertab listings_blank" title="Survey downloads by user">
	<h3>Active users</h3>
	<% unless @active_users.empty? %>
	<table class="values-table">
		<!-- <caption>Downloads in the last year</caption> -->
		<thead>
			<tr><th>User</th><th>Last seen on</th></tr>
		</thead>
		<tbody>
	<% @active_users.each do |user| -%>
		<tr><td><%= user.person.name -%></td><td><%= user.last_user_activity.strftime('%d/%m/%Y @ %H:%M:%S') -%></td></tr>
	<% end -%>
	</tbody>
	</table>
	<% else %>
	No active users
	<% end %>
</div>
</div>
<script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Survey');
        data.addColumn('number', 'downloads');
		<% unless @week.empty? -%>
        data.addRows(<%= @week.length -%>);
		<% @week.each_key do |key| -%>
			data.setValue(<%= week_keys.index(key)-%>, 0, "<%= Survey.find(key).title -%>");
			data.setValue(<%= week_keys.index(key)-%>, 1, <%= @week[key] -%>);
		<% end -%>

        var chart = new google.visualization.ColumnChart(document.getElementById('week_chart_div'));
        chart.draw(data, {width: 800, height: 240, title: 'Survey downloads in last week',
                          vAxis: {title: 'Downloads in the last week', titleTextStyle: {color: 'red'}}
                         , hAxis: {slantedText: true}});
 <% end -%>
      }
    </script>
	<script type="text/javascript">
	      google.load("visualization", "1", {packages:["corechart"]});
	      google.setOnLoadCallback(drawChart);
	      function drawChart() {
	        var data = new google.visualization.DataTable();
	        data.addColumn('string', 'Survey');
	        data.addColumn('number', 'downloads');
			<% unless @month.empty? -%>
	        data.addRows(<%= @month.length -%>);
			<% @month.each_key do |key| -%>
				data.setValue(<%= month_keys.index(key)-%>, 0, "<%= Survey.find(key).title -%>");
				data.setValue(<%= month_keys.index(key)-%>, 1, <%= @month[key] -%>);
			<% end -%>

	        var chart = new google.visualization.ColumnChart(document.getElementById('month_chart_div'));
	        chart.draw(data, {width: 800, height: 240, title: 'Survey downloads in the last month',
	                          vAxis: {title: 'Downloads in the last month', titleTextStyle: {color: 'red'}}
	                         , hAxis: {slantedText: true}});
	 <% end -%>
	      }
	    </script>
		<script type="text/javascript">
		      google.load("visualization", "1", {packages:["corechart"]});
		      google.setOnLoadCallback(drawChart);
		      function drawChart() {
		        var data = new google.visualization.DataTable();
		        data.addColumn('string', 'Survey');
		        data.addColumn('number', 'downloads');
				<% unless @six_months.empty? -%>
		        data.addRows(<%= @six_months.length -%>);
				<% @six_months.each_key do |key| -%>
					data.setValue(<%= six_months_keys.index(key)-%>, 0, "<%= Survey.find(key).title -%>");
					data.setValue(<%= six_months_keys.index(key)-%>, 1, <%= @six_months[key] -%>);
				<% end -%>

		        var chart = new google.visualization.ColumnChart(document.getElementById('six_months_chart_div'));
		        chart.draw(data, {width: 800, height: 240, title: 'Survey downloads in last six months',
		                          vAxis: {title: 'Downloads in the last six months', titleTextStyle: {color: 'red'}}
		                         , hAxis: {slantedText: true}});
		 <% end -%>
		      }
		    </script>
			<script type="text/javascript">
			      google.load("visualization", "1", {packages:["corechart"]});
			      google.setOnLoadCallback(drawChart);
			      function drawChart() {
			        var data = new google.visualization.DataTable();
			        data.addColumn('string', 'Survey');
			        data.addColumn('number', 'downloads');
					<% unless @year.empty? -%>
			        data.addRows(<%= @year.length -%>);
					<% @year.each_key do |key| -%>
						data.setValue(<%= year_keys.index(key)-%>, 0, "<%= Survey.find(key).title -%>");
						data.setValue(<%= year_keys.index(key)-%>, 1, <%= @year[key] -%>);
					<% end -%>

			        var chart = new google.visualization.ColumnChart(document.getElementById('year_chart_div'));
			        chart.draw(data, {width: 800, height: 240, title: 'Survey downloads in last year',
			                          vAxis: {title: 'Downloads in the last year', titleTextStyle: {color: 'red'}}
			                         , hAxis: {slantedText: true}});
			 <% end -%>
			      }
			    </script>